package com.py.credit.controller;

import com.py.credit.common.Page;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.NetworkMdSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 政务一体化网站红黑名单查询controller
 */
@RestController
@RequestMapping(value = "/mdSearch")
public class NetworkMdSearchController {

    @Autowired
    private NetworkMdSearchService networkMdSearchService;

    /**
     * 查询奖惩领域的搜索条件
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @return
     */
    @RequestMapping(value = "/getSearchDomain.json")
    public RestServiceResult getSearchDomain(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb) {
        return new RestServiceResult(networkMdSearchService.selectGzNameList(jclb));
    }

    /**
     * 名单查询分页列表
     *
     * @param jclb      奖惩类别：C:惩戒 J:激励
     * @param type      奖惩对象：C:法人 P:自然人
     * @param remark    奖惩领域
     * @param searchKey 搜索关键字
     * @param page      分页条件
     * @return
     */
    @RequestMapping(value = "/mdList.json")
    public RestServiceResult getMdList(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                       @RequestParam(value = "type", defaultValue = "", required = true) String type,
                                       @RequestParam(value = "remark", defaultValue = "", required = true) String remark,
                                       @RequestParam(value = "searchKey", defaultValue = "", required = true) String searchKey,
                                       Page page) {
        List<LinkedHashMap> mapList = networkMdSearchService.selectZtmcListForPage(jclb, type, remark, searchKey, page);
        return new RestServiceResult(page.getTotalCount(), mapList);
    }

    /**
     * 查询红黑名单数量
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @return
     */
    @RequestMapping(value = "/mdCount.json")
    public RestServiceResult getMdCount(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb) {
        return new RestServiceResult(networkMdSearchService.selectHongheimdCount(jclb,"J"));
    }
    
    
    /**
     * 查询红黑名单数量
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @return
     */
    @RequestMapping(value = "/mdCountnew.json")
    public RestServiceResult mdCountnew(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb) {
    	int tbsl = networkMdSearchService.selectHongheimdCount(jclb,"J");
    	int ycsl = networkMdSearchService.selectHongheimdCount(jclb,"O");
    	Map map = new HashMap<>();
    	map.put("tbsl", tbsl);
    	map.put("ycsl", ycsl);
    	
        return new RestServiceResult(map);
    }

    /**
     * 获取案号列表
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @param tybm 统一编码
     * @param deptId 部门id
     * @return
     */
    @RequestMapping(value = "/ajbhList.json")
    public RestServiceResult getAjbhList(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                         @RequestParam(value = "tybm", defaultValue = "", required = true) String tybm,
                                         @RequestParam(value = "deptId", defaultValue = "", required = true) String deptId) {
        return new RestServiceResult(networkMdSearchService.selectMdList(jclb, tybm, deptId));
    }

    /**
     * 获取案件详情
     *
     * @param ajbh 案号
     * @return
     */
    @RequestMapping(value = "/ajbhDetail.json")
    public RestServiceResult getAjbhDetail(@RequestParam(value = "ajbh", defaultValue = "", required = true) String ajbh) {
        return new RestServiceResult(networkMdSearchService.selectMdInfo(ajbh));
    }

    /**
     * 获取奖惩措施红黑名单
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @param page 分页条件
     * @return
     */
    @RequestMapping(value = "/csMdList.json")
    public RestServiceResult getCsMdList(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                         Page page) {
        List<LinkedHashMap> csMdList = networkMdSearchService.selectCsMdForPage(jclb, page);
        return new RestServiceResult(page.getTotalCount(), csMdList);
    }

    /**
     * 下载各措施对应的红黑名单excel表格
     *
     * @param jclb     奖惩类别：C:惩戒 J:激励
     * @param gzId     规则id
     * @param deptId   部门id
     * @param csId     措施id
     * @param csName   措施名称
     * @param response
     */
    @RequestMapping(value = "/exportExcel.json")
    public void excelExport(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                            @RequestParam(value = "gzId", defaultValue = "", required = true) String gzId,
                            @RequestParam(value = "deptId", defaultValue = "", required = true) String deptId,
                            @RequestParam(value = "csId", defaultValue = "", required = true) String csId,
                            @RequestParam(value = "csName", defaultValue = "", required = true) String csName,
                            HttpServletResponse response) {
        networkMdSearchService.excelExport(response, jclb, gzId, deptId, csId, csName);
    }

    /**
     * 获取所有措施的配合部门及数量
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @param name 规则名称
     * @return
     */
    @RequestMapping(value = "/phDeptNum.json")
    public RestServiceResult getPhDeptNum(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                          @RequestParam(value = "name", defaultValue = "", required = true) String name) {
        return new RestServiceResult(networkMdSearchService.selectPhDeptCsNum(jclb, name));
    }

    /**
     * 查询规则详情
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @param name 规则名称
     * @return
     */
    @RequestMapping(value = "/getGzInfo.json")
    public RestServiceResult getGzInfo(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                       @RequestParam(value = "name", defaultValue = "", required = true) String name) {
        return new RestServiceResult(networkMdSearchService.selectGzInfo(jclb, name));
    }

    /**
     * 更多部门措施
     *
     * @param jclb 奖惩类别：C:惩戒 J:激励
     * @param name 规则名称
     * @param page 分页条件
     * @return
     */
    @RequestMapping(value = "/getGzCs.json")
    public RestServiceResult getGzCs(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                     @RequestParam(value = "name", defaultValue = "", required = true) String name,
                                     Page page) {
        List<LinkedHashMap> mapList = networkMdSearchService.selectGzCsForPage(jclb, name, page);
        return new RestServiceResult(page.getTotalCount(),mapList);
    }
}
